<?php
    session_start();
    require_once 'public/includes/functions.php';
    require_once 'models/container.php';
    
    class ContainerController extends functions {
        
        var $container = null;
        var $row = null;
        var $infoContainer = array();
        var $cat=null;
        
        function __construct(){
            if(empty($_SESSION['name'])){
                echo '<script language="javascript">
                        alert("Vui lòng đăng nhập để vào trang Admin");
                        document.location="index.php";
                    </script>';
            }
            $this->container = new Container_Model(); 
            $this->getCataloge();           
        }
        
        public function listAction(){
            $sql = "select * from container ";
            if(isset($_GET['cbbFilter'])){
                $filter = strtolower($_GET['cbbFilter']);
                switch($filter){
                    case 'danhlam' : 
                        $sql .= " WHERE cat_id = 'danhlam' ";
                        break;
                    case 'ditich' : 
                        $sql .= " WHERE cat_id = 'ditich'";
                        break;
                    case 'khachsan' : 
                        $sql .= " WHERE cat_id = 'khachsan'";
                        break;
                    case 'khudulich' : 
                        $sql .= " WHERE cat_id = 'khudulich' ";
                        break;
                    case 'nhahang' : 
                        $sql .= " WHERE cat_id = 'nhahang' ";
                        break;
                }
            }
            $sql .= " ORDER BY Id DESC";
                       
            $res = $this->container->ExecuteSQL($sql);
            $this->row=mysql_num_rows($res);
            $stt=0;            
            while($rs=mysql_fetch_array($res)){               
                $stt ++;
                $this->infoContainer[]= array(
                        'stt'       => $stt,
                        'id'        => $rs['ID'],
                        'name'      => $rs['name'],
                        'gioithieu' => $rs['gioithieu'],
                        'noidung'   => $rs['noidung'],
                        'anh'       => $rs['anh'],
                        'nguon'     => $rs['nguon'],
                        'diadiem'   => $rs['diadiem'],
                        'lng'       => $rs['kinhdo'],
                        'lat'       => $rs['vido'],
                        'cat_id'    => $rs['cat_id'],
                        'ngaypost'  => $rs['ngaypost']
                    );
                
            }           
        }
        
        public function deleteAction(){
            if(isset($_GET['action']) && strtolower($_GET['action'])=='delete'){
                $id = isset($_GET['id']) ? $_GET['id'] : '';
                $this->container->delete($id);            
            }elseif(!empty($_POST['check']) && isset($_POST['deleteall']) ){
                $arr_del = $_POST['check'];
                for($i = 0;$i< count($arr_del); $i++){
                    $this->container->delete($arr_del[$i]);
                }
            }
        }
        
        private function getCataloge(){
            $this->cat = $this->container->getAll('cataloge');
        }
    }
    
    //action
    $container = new ContainerController();
    //
    //xoa record
    if(isset($_GET['action']) || isset($_POST['deleteall'])){
        $container->deleteAction();
    }
    $container->listAction();
    $container->show('admin');
    
?>